package cn.ava.ymcc.service.impl;

import cn.ava.ymcc.constants.CommonConstants;
import cn.ava.ymcc.domain.CourseMarket;
import cn.ava.ymcc.domain.CourseUserLearn;
import cn.ava.ymcc.dto.PayNotifyDto;
import cn.ava.ymcc.mapper.CourseUserLearnMapper;
import cn.ava.ymcc.result.JSONResult;
import cn.ava.ymcc.service.ICourseMarketService;
import cn.ava.ymcc.service.ICourseUserLearnService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ava
 * @since 2022-08-09
 */
@Service
@Slf4j
public class CourseUserLearnServiceImpl extends ServiceImpl<CourseUserLearnMapper, CourseUserLearn> implements ICourseUserLearnService {

    @Autowired
    private ICourseMarketService courseMarketService;

    /**
     * 校验用户是否购买此课程
     * @param loginId
     * @param courseId
     * @return
     */
    @Override
    public CourseUserLearn checkUser(Long loginId, Long courseId) {
        Wrapper<CourseUserLearn> query = new EntityWrapper<>();
        query.eq("login_id",loginId);
        query.eq("course_id",courseId);
        return selectOne(query);
    }

    @Override
    @Transactional
    public void savaOrUpdateUserLearn(PayNotifyDto notifyDto) {
        Map<String,Object> map = JSONObject.parseObject(notifyDto.getExtParams(), Map.class);
        Long loginId = Long.valueOf(map.get("loginId").toString());
        String idStr = map.get("courseId").toString();
        String[] ids = idStr.split(",");
        // 根据id查询出用户购买记录
        Date now = new Date();
        for (String id : ids) { // 课程有多个
            Long courseId = Long.valueOf(id);
            CourseUserLearn courseUserLearn = checkUser(loginId, courseId);
            // 查询课程购买天数
            CourseMarket courseMarket = courseMarketService.selectById(courseId);
            if (courseUserLearn == null) { // 如果为空直接添加记录
                courseUserLearn = new CourseUserLearn();
                courseUserLearn.setCourseId(courseId);
                courseUserLearn.setCourseOrderNo(notifyDto.getOrderNo());
                courseUserLearn.setCreateTime(now);
                courseUserLearn.setStartTime(now);
                courseUserLearn.setEndTime(DateUtils.addDays(now,courseMarket.getValidDays()));
                courseUserLearn.setLoginId(loginId);
                log.info("添加用户购买记录");
                insert(courseUserLearn);
            }else { // 存在直接增加天数
                courseUserLearn.setCourseOrderNo(notifyDto.getOrderNo());
                courseUserLearn.setEndTime(DateUtils.addDays(courseUserLearn.getEndTime(),courseMarket.getValidDays()));
                courseUserLearn.setState(CommonConstants.CourseStateConstant.STATE_ZERO);
                 updateById(courseUserLearn);
                log.info("增加用户购买天数");
            }
        }

    }
}
